package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.ServiceProviderDTO;
import com.sky.dto.ServiceProviderPageQueryDTO;
import com.sky.entity.ServiceProvider;
import com.sky.entity.ServiceSkill;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.ServiceProviderMapper;
import com.sky.mapper.ServiceSkillMapper;
import com.sky.mapper.PackageProviderMapper;
import com.sky.result.PageResult;
import com.sky.service.ServiceProviderService;
import com.sky.vo.ServiceProviderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ServiceProviderServiceImpl implements ServiceProviderService {
    @Autowired
    private ServiceProviderMapper serviceProviderMapper;
    @Autowired
    private ServiceSkillMapper serviceSkillMapper;
    @Autowired
    private PackageProviderMapper packageProviderMapper;

    /**
     * 新增服务人员和对应技能
     * @param serviceProviderDTO
     */
    @Transactional
    public void addWithSkill(ServiceProviderDTO serviceProviderDTO) {
        ServiceProvider serviceProvider = new ServiceProvider();
        BeanUtils.copyProperties(serviceProviderDTO, serviceProvider);
        // 插入一条服务人员数据
        serviceProviderMapper.insert(serviceProvider);
        // 获取insert服务人员生成的主键值
        Long providerId = serviceProvider.getId();
        // 插入n条技能数据
        List<ServiceSkill> skills = serviceProviderDTO.getSkills();
        if (skills != null && !skills.isEmpty()) {
            skills.forEach(serviceSkill -> {
                serviceSkill.setProviderId(providerId);
            });
            serviceSkillMapper.insertBatch(skills);
        }
    }

    /**
     * 分页查询服务人员
     */
    public PageResult pageQuery(ServiceProviderPageQueryDTO serviceProviderPageQueryDTO) {
        PageHelper.startPage(serviceProviderPageQueryDTO.getPage(), serviceProviderPageQueryDTO.getPageSize());
        Page<ServiceProviderVO> page = serviceProviderMapper.pageQuery(serviceProviderPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * (批量)删除服务人员
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        // 判断服务人员是否在启用状态
        for (Long id : ids) {
            ServiceProvider serviceProvider = serviceProviderMapper.getById(id);
            if (serviceProvider.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        // 判断服务人员是否在套餐中
        List<Long> packageIds = packageProviderMapper.getPackageIdByProviderIds(ids);
        if (packageIds != null && packageIds.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        // 批量删除服务人员数据
        serviceProviderMapper.deleteByIds(ids);
        // 批量删除技能数据
        serviceSkillMapper.deleteByProviderIds(ids);
    }

    /**
     * 根据id查询服务人员
     * @param id
     * @return ServiceProviderVO
     */
    public ServiceProviderVO getServiceProviderVOById(Long id) {
        ServiceProvider serviceProvider = serviceProviderMapper.getById(id);
        List<ServiceSkill> serviceSkills = serviceSkillMapper.getByProviderId(id);

        ServiceProviderVO serviceProviderVO = new ServiceProviderVO();
        BeanUtils.copyProperties(serviceProvider, serviceProviderVO);
        serviceProviderVO.setSkills(serviceSkills);

        return serviceProviderVO;
    }

    /**
     * 修改服务人员
     * @param serviceProviderDTO
     */
    public void updateWithSkill(ServiceProviderDTO serviceProviderDTO) {
        ServiceProvider serviceProvider = new ServiceProvider();
        BeanUtils.copyProperties(serviceProviderDTO, serviceProvider);
        // 修改服务人员基本信息
        serviceProviderMapper.update(serviceProvider);
        // 删除技能数据
        serviceSkillMapper.deleteByProviderId(serviceProvider.getId());
        // 重新插入技能数据
        List<ServiceSkill> skills = serviceProviderDTO.getSkills();
        if (skills != null && !skills.isEmpty()) {
            skills.forEach(serviceSkill -> {
                serviceSkill.setProviderId(serviceProviderDTO.getId());
            });
            serviceSkillMapper.insertBatch(skills);
        }
    }

    /**
     * 启用停用服务人员
     * @param status
     * @param id
     */
    public void startOrStopProvider(Integer status, Long id) {
        ServiceProvider serviceProvider = ServiceProvider.builder().status(status).id(id).build();
        serviceProviderMapper.updateStatus(serviceProvider);
    }

    /**
     * 根据分类id查询服务人员
     * @param categoryId
     * @return
     */
    @Override
    public List<ServiceProvider> list(Long categoryId) {
        ServiceProvider serviceProvider = ServiceProvider.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return serviceProviderMapper.list(serviceProvider);
    }

    /**
     * 条件查询服务人员和技能
     * @param serviceProvider
     * @return
     */
    public List<ServiceProviderVO> listWithSkill(ServiceProvider serviceProvider) {
        List<ServiceProvider> serviceProviderList = serviceProviderMapper.list(serviceProvider);

        List<ServiceProviderVO> serviceProviderVOList = new ArrayList<>();

        for (ServiceProvider sp : serviceProviderList) {
            ServiceProviderVO serviceProviderVO = new ServiceProviderVO();
            BeanUtils.copyProperties(sp, serviceProviderVO);

            // 根据服务人员id查询对应的技能
            List<ServiceSkill> skills = serviceSkillMapper.getByProviderId(sp.getId());

            serviceProviderVO.setSkills(skills);
            serviceProviderVOList.add(serviceProviderVO);
        }

        return serviceProviderVOList;
    }

    /**
     * 根据手机号查询服务人员
     * @param phone
     * @return
     */
    @Override
    public ServiceProvider getByPhone(String phone) {
        return serviceProviderMapper.getByPhone(phone);
    }

    /**
     * 根据用户名查询服务人员
     * @param username
     * @return
     */
    @Override
    public ServiceProvider getByUsername(String username) {
        return serviceProviderMapper.getByUsername(username);
    }
} 